package com.appcenter.message.expand;

import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.message.mapper.DingConfigsMapper;
import com.appcenter.message.model.DingConfigs;
import com.appcenter.message.service.HttpAPIService;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DingDingMsg {
    @Autowired
    DingConfigsMapper dingConfigMapper;
    @Autowired
    private HttpAPIService httpAPIService;

    @Autowired
    ObjectMapper objectMapper;

    public String Send(String to, String subject, String body, String url, String HeadText, String AppCode) throws Exception {

        UserDingSenderConfiguration curUserDingSenderConfiguration = GetDingConfig(AppCode);

        if (curUserDingSenderConfiguration == null) {
            throw new ErrorJsonResult("1000015", "未找到配置信息,AppCode:" + AppCode);
        }
        to = to.replace(',', '|');
        to = to.substring(0,to.length()-1);

        String urlGetDingToken = curUserDingSenderConfiguration.TokenUrl;
        String responseGetDingToken = httpAPIService.doGet(urlGetDingToken);
        DingToken dingToken = objectMapper.readValue(responseGetDingToken, DingToken.class);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String sendTime = df.format(new Date());
        String postJson = "";

        List<DingNoticeOAKeyValue> curlist = new ArrayList<DingNoticeOAKeyValue>();
        DingNoticeOAKeyValue curDingNoticeOAKeyValue = new DingNoticeOAKeyValue();
        curDingNoticeOAKeyValue.key = "发送时间：";
        curDingNoticeOAKeyValue.value = sendTime;
        curlist.add(curDingNoticeOAKeyValue);

        DingNoticeOABody curbody = new DingNoticeOABody();
        curbody.title = subject;
        curbody.content = body;
        curbody.form = curlist;

        DingNoticeOAHead curhead = new DingNoticeOAHead();
        curhead.OAHeadText = HeadText;
        curhead.bgcolor = "FF37A0D9";

        DingNoticeOA curDingNoticeOA = new DingNoticeOA();
        curDingNoticeOA.message_url = url;
        curDingNoticeOA.head = curhead;
        curDingNoticeOA.body = curbody;

        DingNoticeOAMsg postOAMsg = new DingNoticeOAMsg();
        postOAMsg.touser = to;
        postOAMsg.agentid = curUserDingSenderConfiguration.agentId;
        postOAMsg.oa = curDingNoticeOA;
        postOAMsg.msgtype="oa";
        postJson = objectMapper.writeValueAsString(postOAMsg);
        String urlSendDingNotice = String.format(curUserDingSenderConfiguration.PostDingNoticeUrl, dingToken.AccessToken);
        String responseJson = httpAPIService.JsonSMS(urlSendDingNotice, postJson);

        return responseJson;
    }

    public UserDingSenderConfiguration GetDingConfig(String AppCode) {

        DingConfigs curDingConfig = dingConfigMapper.getDingConfig(AppCode);
        if (curDingConfig == null) {
            return null;
        } else {
            UserDingSenderConfiguration curUserDingSenderConfiguration = new UserDingSenderConfiguration();
            curUserDingSenderConfiguration.agentId = curDingConfig.getAgentId();
            curUserDingSenderConfiguration.OAHeadBgColor = curDingConfig.getOAHeadBgColor();
            curUserDingSenderConfiguration.PostDingNoticeUrl = curDingConfig.getPostDingNoticeUrl();
            curUserDingSenderConfiguration.TokenUrl = curDingConfig.getTokenUrl();
            return curUserDingSenderConfiguration;
        }
    }

}
